package com.tjetc.view;

import com.tjetc.domain.*;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.CustomerService;
import com.tjetc.service.OrderService;
import com.tjetc.service.OrderSettlementService;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

/**
 * 用户管理界面
 */
public class CustomerManageView {
    private static Scanner sc=new Scanner(System.in);
    private CustomerService customerService= ClientProxy.getProxyInstance(CustomerService.class);
    private OrderService orderService=ClientProxy.getProxyInstance(OrderService.class);
    private OrderSettlementService orderSettlementService=ClientProxy.getProxyInstance(OrderSettlementService.class);
    public void start() {
        while (true){
            System.out.println("🥰🥰~~~~~~客户管理页面~~~~~~🥰🥰");
            System.out.println("1.客户列表");
            System.out.println("2.添加客户");
            System.out.println("3.修改客户信息");
            System.out.println("4.删除客户");
            System.out.println("5.查看客户订单记录");
            System.out.println("0.返回上一级");
            System.out.println("请输入你的选择");
            int i=sc.nextInt();
            switch (i){
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    delete();
                    break;
                case 5:
                    getOrderByCid();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误请重新输入");

            }
        }
    }

    /**
     * 根据客户id查询订单
     */
    private void getOrderByCid() {
        /**
         * 先列出所有客户
         */
        list();
        System.out.println("请选择你想查看的客户id");
        int id = sc.nextInt();
        Result result=customerService.selectById(id);
        if (result.getCode()==200){
            List<Order> list = orderService.findByCustomerId(id);
            /**
             * 如果订单列表为空，则打印订单为空
             */
            if (list==null ||list.size()==0){
                System.out.println("当前客户的订单为空!");
            }else {
                for (Order order : list) {
                    Customer customer = order.getCustomer();
                    CustomerMessage customerMessage = customer.getCustomerMessage();
                    Vehicle vehicle = order.getVehicle();
                    Integer orderStatus = order.getOrderStatus();
                    //根据用户订单状态分别打印
                    //如果用户订单状态已经结束，则打印订单总费用
                    if(orderStatus==3){
                        OrderSettlement orderSettlement = orderSettlementService.findByOrderIdLink(order.getOrderId());
                        Order order1 = orderSettlement.getOrder();
                        Customer customer1 = order1.getCustomer();
                        CustomerMessage customerMessage1 = customer1.getCustomerMessage();
                        Vehicle vehicle1 = order1.getVehicle();
                        System.out.println("订单ID:"+order1.getOrderId()+
                                ",客户的真实姓名:"+customerMessage1.getName()+
                                ",车辆的型号:"+vehicle1.getModel()+
                                ",车辆的颜色:"+vehicle1.getColor()+
                                ",车牌号:"+vehicle1.getLicensePlate()+
                                ",租车开始的日期:"+order1.getRentalStartDate()+
                                ",租车结束的日期:"+orderSettlement.getActualReturnDate()+
                                ",额外的费用:"+orderSettlement.getAdditionalCharges()+
                                ",订单总费用:"+orderSettlement.getFinalCost()+
                                ",订单结算日期:"+orderSettlement.getSettlementDate());
                    }
                    //如果订单状态为未完成状态，则打印预计总费用
                    else {
                        System.out.println("订单ID:"+order.getOrderId()+
                                ",客户账户:"+customer.getAccount()+
                                ",客户的真实姓名:"+customerMessage.getName()+
                                ",车辆的型号:"+vehicle.getModel()+
                                ",车辆的颜色:"+vehicle.getColor()+
                                ",车牌号:"+vehicle.getLicensePlate()+
                                ",租车开始的日期:"+order.getRentalStartDate()+
                                ",租车结束预计的日期:"+order.getRentalEndDate()+
                                ",订单状态:"+(orderStatus==0?"待审核":orderStatus==1?"已确认":orderStatus==2?"进行中":"已完成")+
                                ",订单预计总费用:"+order.getTotalCost()+
                                ",订单提交日期:"+order.getOrderDate());
                    }
                }
            }
        }
        //如果没有用户，则打印无
        else {
            System.out.println("没有此客户!");
        }
    }

    /**
     * 根据用户id删除用户
     */
    private void delete() {
        //列出所有用户
        list();
        System.out.println("请输入想要删除的用户id");
        int id=sc.nextInt();
        //根据id删除
        Result result=customerService.deleteById(id);
        System.out.println(result.getMsg());
    }

    /**
     * 更新客户信息
     */
    private void update() {
        //列出所有用户
        list();
        System.out.println("请输入用户id");
        int id= sc.nextInt();
        System.out.println("请输入用户名");
        String account = sc.next();
        System.out.println("请输入密码");
        String password=sc.next();
        /*System.out.println("请输入姓名");
        String name = sc.next();
        String phone = null;
        while (true) {
            System.out.println("请输入手机号");
            phone = sc.next();
            if (!phone.matches("1[35789]//d{9}")){
                System.out.println("输入错误");
            }else {
                break;
            }
        }*/
        //创建对象
        Customer customer=new Customer(id,account,password,null);
        Result result=customerService.update(customer);
        System.out.println(result.getMsg());
    }

    /**
     * 列出所有用户
     */
    private void list() {
        Result result=customerService.list();
        List<Customer> list=(List<Customer>) result.getData();
        for (Customer customer : list) {
            System.out.print("用户的ID:"+customer.getCustomerId()
            +",用户的账户名称:"+customer.getAccount()+",用户的账户密码:"+customer.getPassword());
            if (customer.getCustomerMessage()!=null){
                System.out.println(",姓名:"+customer.getCustomerMessage().getName()+",手机号"+customer.getCustomerMessage().getPhone());
            }else {
                System.out.println("");
            }
        }
    }

    /**
     *添加用户
     */
    private void add() {
        System.out.println("请输入用户名");
        String account = sc.next();
        System.out.println("请输入密码");
        String password=sc.next();
       /* System.out.println("请输入姓名");
        String name = sc.next();
        String phone = null;
        while (true) {
            System.out.println("请输入手机号");
            phone = sc.next();
            if (!phone.matches("1[35789]//d{9}")){
                System.out.println("输入错误");
            }else {
                break;
            }
        }*/
        //创建对象
        Customer customer=new Customer();
        customer.setAccount(account);
        customer.setPassword(password);
        Result result=customerService.register(customer);
        System.out.println(result.getMsg());
    }
}
